#include "log.h"
#include <assert.h>
#include <stdio.h>
#include "html_inc.h"

#if   defined(_MSC_VER) && (_MSC_VER>1000)
#pragma warning(disable:996 267)
#endif

// Check if it is a html log file
#define HTML_ASSERT()   {assert(g_filetype | RAYLOG_TYPE_HTML);   \
                         assert(g_log); };

extern unsigned    g_filetype;
extern FILE*       g_log;
extern unsigned    g_itemcolor;
extern unsigned    g_enabled;

const char* colors[RAYLOG_COLOR_COUNT] =
{
    "FF0000",
    "00FF00",
    "0000FF",
    "000000"
};

const char* mark[RAYLOG_FORMAT_COUNT] =
{
    "<title>%s</title>",                                // title
    "<p class=""header"" align=""center"">%s</p>",      // header
    "<p class=""mainheader"" align=""left"">%s</p>",    // subheader
    "<p class=""maincontent"" align=""left"">%s</p>",   // maincontent
    "<li>%s</li>"                                       // list
};

const char* cmark[RAYLOG_FORMAT_COUNT] =
{
    "<font color=""%s""> <title>%s</title> </font>",    // title
    "<font color=""%s""> <p class=""header"" align=""center"">%s</p> </font>",
                                // header
    "<font color=""%s""> <p class=""mainheader"" align=""left"">%s</p> </font>",
                                // subheader
    "<font color=""%s""> <p class=""maincontent"" align=""left"">%s</p> </font>",
                                // maincontent
    "<font color=""%s""> <li>%s</li> </font>"         // list
};

void _loadHtmlInc()
{
    FILE *html;
    char buff[256];
    int count = 0;
/*
    HTML_ASSERT();

    html = fopen("./html_inc.txt","r");
    if (!html)
    {
    fprintf(stderr, "Can't find include file for html.\n");
    assert(0);
    }

    while ((count = fread(buff, sizeof(char), 256, html)) == 256)
        fwrite(buff, sizeof(char), 256, g_log);
    fwrite(buff, sizeof(char), count, g_log);

    fclose(html);
*/
    fwrite(html_inc, sizeof(char), sizeof(html_inc), g_log);
}

void _writeHtml(char* content, unsigned fmttype)
{
    HTML_ASSERT();
    assert( fmttype < RAYLOG_FORMAT_COUNT );
    CHECK_ENABLE();
    fprintf(g_log, mark[fmttype], content);
    fflush(g_log);
    fprintf(g_log,"\n");
}

void _cwriteHtml(char* content, unsigned fmttype, unsigned cltype)
{
    HTML_ASSERT();
    assert( fmttype < RAYLOG_FORMAT_COUNT );
    assert( cltype < RAYLOG_LEVEL_COUNT );
    CHECK_ENABLE();
    fprintf(g_log, cmark[fmttype], colors[cltype], content);
    fprintf(g_log,"\n");
}
